<?php
namespace Shopmanage\Controller;
use Think\Controller;
class FrontcfgController extends ExtendController {
	public function anmicfg_save(){
		$status = I("status");
		$data["none_anim"]=(int)$status;
		$SFG = M("ShopCfg");
		$re["status"] = $SFG->where("`sid`=".$this->sid)->save($data);
		$re["anmicfg"]=$status;
		$this->ajaxReturn($re);
	}
	public function bandcfg_save(){
		$gifts = I("gift");
		$width = I("width");
		$height = I("height");
		$left = I("left");
		$top = I("top");
		$forbidden = I("forbidden");
		$showpic = I("showpic");
		$kaiguan = I("kaiguan");
		$uploadbg = I("uploadbg");
		$GFT = M("EventsGifts");
		$gift_pics = $GFT->where("`sid`=".$this->sid." AND `status`<>9")->getField("id,pic_url");//print_r($gift_pics);
		foreach($gifts as $k=>$gift){
			$tmp["giftset"][$k]["gift"]=$gifts[$k];
			$tmp["giftset"][$k]["width"]=$width[$k];
			$tmp["giftset"][$k]["height"]=$height[$k];
			$tmp["giftset"][$k]["left"]=$left[$k];
			$tmp["giftset"][$k]["top"]=$top[$k];
			$tmp["giftset"][$k]["forbidden"]=$forbidden[$k]==1?true:false;
			$tmp["giftset"][$k]["pic_url"]=$showpic[$k]==1?$gift_pics[$gifts[$k]]:"";
		}
		$tmp["onoff"]=$kaiguan;
		$data["cfg"] =  json_encode($tmp);
		
		$SHOP = M("ShopCfg");
		$curtool = $SHOP->where("`sid`=".$this->sid)->getField("curtool");
		$CHKT = D("CheckTools");
		$toolid = $CHKT->GetToolID($curtool);
		
		$CFG = M("ToolsStatus");
		$CFG->where("`sid`=".$this->sid." AND `tool_id`=".$toolid)->save($data);
		
		$da["front_cfg"]=I("code");
		if($uploadbg){
			$da["front_pic_url"]=$uploadbg;
			$da["front_pic"]=1;
		}
		$CFG = M("ShopCfg");
		$re = $CFG->where("`sid`=".$this->sid)->save($da);
		$this->success("保存成功！","/?a=bandcfg&m=shopmanage&c=frontcfg");
	}
	public function bandcfg(){
		$mob = I("mob");
		$SHOP = M("ShopCfg");
		$cfg["frontcfg"] = current($SHOP->where("`sid`=".$this->sid)->getField("id,curtool,none_anim,front_pic,front_pic_url,front_cfg"));
		if(!$cfg["frontcfg"]["curtool"]){
			$this->success("抱歉，您还没选择活动类型，请先选择活动类型再来！","/?a=gamecfg&m=shopmanage");
			die();
		}
		
		$CFG = M("ToolsStatus");
		$CHKT = D("CheckTools");
		$toolid = $CHKT->GetToolID($cfg["frontcfg"]["curtool"]);
		$re = $CFG->where("`sid`=".$this->sid." AND `tool_id`=".$toolid)->getField("cfg");
		$giftset = json_decode($re,true);
		if(!$giftset["giftset"]){
			$giftset = json_decode(C("GIFTS_DEFAULT")[$cfg["frontcfg"]["curtool"]],true);
		}
		$cfg["giftset"] = $giftset["giftset"];
		$cfg["onoff"] = $giftset["onoff"];
		
		$GFT = M("EventsGifts");
		$gifts = $GFT->where("`sid`=".$this->sid." AND `status`<>9")->getField("id,type,level,name,status");
		$gifts["0"]["name"]="不中奖";
		foreach($gifts as $k=>$gift){
			$gifts[$k]["level"]=getlevel($gifts[$k]["level"]);
		}
		if(!$cfg["frontcfg"]["front_cfg"]){
			if(C("FRONT_DEFAULT")[$cfg["frontcfg"]["curtool"]]){
				$cfg["frontcfg"]["front_cfg"]= json_decode(C("FRONT_DEFAULT")[$cfg["frontcfg"]["curtool"]],true);
			}else{
				$cfg["frontcfg"]["front_cfg"]= json_decode(C("FRONT_DEFAULT")["default"],true);
			}
		}else{
			$issetted = 1;
			$cfg["frontcfg"]["front_cfg"]= json_decode(htmlspecialchars_decode($cfg["frontcfg"]["front_cfg"]),true);
		}
		if(!$cfg["frontcfg"]["button_cfg"]){
			$cfg["frontcfg"]["button_cfg"] = json_decode(C("BUTTON_DEFAULT"),true);
		}
		if(C("FRONT_DEFAULT")[$cfg["frontcfg"]["curtool"]]){
			$frount_default = (json_decode(C("FRONT_DEFAULT")[$cfg["frontcfg"]["curtool"]],true));
		}else{
			$frount_default = (json_decode(C("FRONT_DEFAULT")["default"],true));
		}
		$info["type"]="warning";
		$info["msg"]="亲，在进行操作时请先确认对店铺没有影响。";
		$this->assign("info",$info);
		$this->assign("issetted",$issetted);
		$this->assign("sid",$this->sid);
		$this->assign("bg_default",C("DEFAULT_FRONT_PIC_URL")[$cfg["frontcfg"]["curtool"]]);
		$this->assign("frount_default",$frount_default);
		$this->assign("gift_default",json_decode(C("DICES_DEFAULT"),true));
		$this->assign("cfg",$cfg);
		$this->assign("gifts",$gifts);
		$this->assign("title","绑定奖品");
		$this->assign("current","waiguan");
		$this->display();
	}
	public function gamewg_save_kg(){
		$eid = I("eid");
		if($eid){
			$data["front_pic"]=I("get.status");
			$data["front_pic_url"]=I("get.picurl");
			$CFG = M("Events");
			$data["status"] = $CFG->where("`sid`=".$this->sid." AND `id`=".$eid)->save($data);
		}else{
			$data["front_pic"]=I("get.status");
			$data["front_pic_url"]=I("get.picurl");
			$CFG = M("ShopCfg");
			$data["status"] = $CFG->where("`sid`=".$this->sid)->save($data);
		}
		$this->ajaxReturn($data);
	}
	public function gamewg_save(){
		$data["front_pic_url"]=I("post.urls");
		$data["front_cfg"]=I("post.code");
		$CFG = M("ShopCfg");
		$re = $CFG->where("`sid`=".$this->sid)->save($data);
		if($re){
			$this->success("保存成功！","/?a=gamewg&m=shopmanage&c=frontcfg");
		}else{
			// echo $CFG->getDbError();
			$this->assign("message","请检查以下项目：<br>1.请确认您配置过活动，只有先配置活动，才可以自定义图片。<br />2.您没有重新上传文件，仍然使用原来的图片。");
			$this->error("保存失败！","/?a=gamewg&m=shopmanage");
		}
	}
	// public function gamewg(){
	// // 外观设置
	// 拖拽上线时已废弃
		// $CFG = M("ShopCfg");
		// $cfg = current($CFG->where("`sid`=".$this->sid)->getField("curtool,none_anim,front_pic,front_pic_url,front_cfg"));
		// $bg_own = $cfg["front_pic_url"];
		// switch($cfg["curtool"]){
			// case "eggs" :
			// $default_cfg["contdown"]["top"]="128";
			// $default_cfg["contdown"]["left"]="11";
			// $default_cfg["action"]["background"]="http://img02.taobaocdn.com/imgextra/i2/27390555/TB2QP30bpXXXXaXXpXXXXXXXXXX-27390555.png";
			// $default_cfg["action"]["width"]="189";
			// $default_cfg["action"]["height"]="303";
			// $default_cfg["action"]["left"]="382";
			// $default_cfg["action"]["top"]="20";
			// $bg = "http://img01.taobaocdn.com/imgextra/i1/27390555/TB2p0vwaVXXXXcVXXXXXXXXXXXX-27390555.jpg";
			// break;
			// case "chest" :
			// $bg = "http://img02.taobaocdn.com/imgextra/i2/27390555/TB2V81oaVXXXXcMXXXXXXXXXXXX-27390555.jpg";
			// break;
			// case "wheels" :
			// $bg = "http://img04.taobaocdn.com/imgextra/i4/27390555/TB2YXhPaVXXXXcXXXXXXXXXXXXX-27390555.jpg";
			// break;
			// case "dices":
			// $default_cfg["contdown"]["top"]="148";
			// $default_cfg["contdown"]["left"]="5";
			// $default_cfg["action"]["background"]="http://img02.taobaocdn.com/imgextra/i2/27390555/TB2WhpOaXXXXXcTXXXXXXXXXXXX-27390555.png";
			// $default_cfg["action"]["width"]="100";
			// $default_cfg["action"]["height"]="100";
			// $default_cfg["action"]["left"]="415";
			// $default_cfg["action"]["top"]="118";
			// $bg = "http://img03.taobaocdn.com/imgextra/i3/27390555/TB22sOiaVXXXXb1XXXXXXXXXXXX-27390555.jpg";
			// break;
			// case "scratchcards":
			// $bg = "http://img04.taobaocdn.com/imgextra/i4/27390555/TB2wTmhaVXXXXcJXXXXXXXXXXXX-27390555.jpg";
			// break;
			// case "fruit":
			// $bg = "http://img03.taobaocdn.com/imgextra/i3/27390555/TB25v1paVXXXXcSXXXXXXXXXXXX-27390555.jpg";
			// break;
			// case "miao":
			// $bg = "http://img04.taobaocdn.com/imgextra/i4/27390555/TB2Rw40apXXXXbjXXXXXXXXXXXX-27390555.jpg";
			// break;
		// }
		// $front_cfg = json_decode(htmlspecialchars_decode($cfg["front_cfg"]),true);
		// $front_cfg["action"]["background"]=str_replace(") no-repeat","",str_replace("url(","",$front_cfg["action"]["background"]));
		// $front_cfg["cart"]["background"]=str_replace(") no-repeat","",str_replace("url(","",$front_cfg["cart"]["background"]));
		// $this->assign("default_cfg",$default_cfg);
		// $this->assign("front_cfg",$front_cfg);
		// $this->assign("curtool",$cfg["curtool"]);
		// $this->assign("bg",$bg);
		// $this->assign("bg_own",$bg_own);
		// $this->assign("bg_open",$cfg["front_pic"]);
		// $this->assign("none_anim",$cfg["none_anim"]);
		// $this->assign("title","外观设置");
		// $this->assign("current","waiguan");
		// $this->display();
	// }
	
}